<?php
    // 接受前端发送过来的数据
    $username = $_POST["username"];
    $password = $_POST["password"];
    $nickname = $_POST["nickname"];
    $sex = $_POST["sex"];
    // 随机ID
     $id = (int)rand(1000,9999);
    // 链接数据库
    $conn = mysqli_connect("localhost", "root", "123456", "sbeam");
    // 插入
    mysqli_query($conn,"insert into users (username,password,sex,id,nickname) values('$username','$password','$sex','$id','$nickname');");
    mysqli_query($conn,"insert into cart (id,cart) values('$id','[]');");
    
    // 判断影响了几行
    $num = mysqli_affected_rows($conn);
    // echo $num;
    if ($num===-1) {
        echo json_encode(array("error" => 1, "data" => "注册失败"));
    } else {
        echo json_encode(array("error" => 0, "data" => "注册成功"));
    }
?>